package _130_Surrounded_Regions;

/**
 * Created by 孙加辉 on 2018/7/22.
 */
class Solution {
    public void solve(char[][] board) {
        if(board.length==0||board[0].length==0)
            return;
        for(int i=0;i<board[0].length;i++){
            if(board[0][i]=='O')
                changeToPoint(board,0,i);
            if(board[board.length-1][i]=='O')
                changeToPoint(board,board.length-1,i);
        }
        for(int i=1;i<board.length-1;i++){
            if(board[i][0]=='O')
                changeToPoint(board,i,0);
            if(board[i][board[0].length-1]=='O')
                changeToPoint(board,i,board[0].length-1);
        }
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                board[i][j] = board[i][j]=='.'?'O':'X';
            }
        }
    }
    public void changeToPoint(char[][] board,int x,int y){
        board[x][y] = '.';
        if(x>0&&board[x-1][y]=='O')
            changeToPoint(board,x-1,y);
        if(x<board.length-1&&board[x+1][y]=='O')
            changeToPoint(board,x+1,y);
        if(y>0&&board[x][y-1]=='O')
            changeToPoint(board,x,y-1);
        if(y<board[0].length-1&&board[x][y+1]=='O')
            changeToPoint(board,x,y+1);
    }
}
public class _130_Surrounded_Regions {
    public static void main(String[] args){

    }
}
